//
//  ZSMenuItemsView.m
//  ZhuShou
//
//  Created by Dejun Liu on 15/10/29.
//  Copyright © 2015年 jw. All rights reserved.
//

#import "ZSMenuItemsView.h"

@interface ZSMenuItemsView()

/**
 *  存放按钮用
 */
@property (nonatomic,strong) UIScrollView *scrollView;

@end

@implementation ZSMenuItemsView

- (instancetype)initWithFrame:(CGRect)frame withItems:(NSMutableArray *)items
{
    self = [super initWithFrame:frame];
    if (self) {
        self.scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
        [self addSubview:self.scrollView];
        
        self.items = items;
        [self makeButtons];
    }
    return self;
}

- (void)makeButtons
{
    if (self.items.count) {
        [self.items enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            ZSMenuItem *item = obj;
            
            ZSMenuItemButton *button = [ZSMenuItemButton buttonWithType:UIButtonTypeCustom];
            [button setTitle:item.name forState:UIControlStateNormal];
            [button setTitleColor:[UIColor df_BlueColor] forState:UIControlStateSelected];
            [button setTitleColor:[UIColor df_GrayDarkColor] forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:14];
            [button setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateSelected];
            [button setBackgroundImage:[UIImage imageWithColor:[UIColor df_GrayColor]] forState:UIControlStateSelected];
            [button setBackgroundImage:[UIImage imageWithColor:[UIColor df_GrayColor]] forState:UIControlStateHighlighted];
            [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            [self.scrollView addSubview:button];
            
            button.item = item;
        }];
    }
    
    [self setLayoutOfButtons];
}

- (void)setLayoutOfButtons
{
    float btnWidth = 60.0f;
    float btnHeight = 25.0f;
    float space = 10.0f;
    float leftRightSpace = 5.0f;
    
    [self.scrollView.subviews enumerateObjectsUsingBlock:^(__kindof UIView *obj, NSUInteger idx, BOOL *stop) {
        ZSMenuItemButton *button = obj;
        float btnLeft = leftRightSpace +(btnWidth+space) * idx;
        button.height = btnHeight;
        button.width = btnWidth;
        button.left = btnLeft+space/2;
        button.centerY = self.scrollView.centerY;
        button.layer.cornerRadius = 6.0f;
        button.clipsToBounds = YES;
        button.layer.borderColor = [UIColor df_GrayColor].CGColor;
        button.layer.borderWidth = 0.5;
    }];
    
    float width = leftRightSpace*2+(space + btnWidth)*self.scrollView.subviews.count;
    width = width<self.scrollView.width?self.scrollView.width:width;
    self.scrollView.contentSize = CGSizeMake(width,self.scrollView.height);
}

- (void)buttonAction:(ZSMenuItemButton *)button
{
    if (self.actionHandler) {
        self.actionHandler(button,button.item);
    }
    [self setSelectWithItem:button];
}

- (void)setSelectWithItem:(ZSMenuItemButton *)button
{
    NSInteger index = [self.scrollView.subviews indexOfObject:button];
    [self setSelectIndex:index];
}

- (void)setSelectIndex:(NSInteger) index
{
    NSArray *subViews = self.scrollView.subviews;
    [subViews enumerateObjectsUsingBlock:^(__kindof UIView *obj, NSUInteger idx, BOOL *stop) {
        ZSMenuItemButton *button = obj;
        if ([obj isKindOfClass:[UIButton class]]) {
            if (idx == index) {
                [button setSelected:YES];
            }else
            {
                [button setSelected:NO];
            }
        }
    }];
}

@end
